#!/usr/bin/ruby
require "rubygems"
require "opengl"
require "glut"
require "m3g"
require "pp"


class MyGLUTWidget
  def initialize
    GLUT.Init
    GLUT.InitWindowSize  256, 256
    GLUT.InitDisplayMode GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH
    GLUT.CreateWindow "Ruby-M3G"

    initialize_scene

    GLUT.ReshapeFunc  method(:reshape).to_proc()
    GLUT.DisplayFunc  method(:display).to_proc()
    GLUT.KeyboardFunc method(:keyboard).to_proc()
    GLUT.MouseFunc    method(:mouse).to_proc()
    GLUT.IdleFunc     method(:idle).to_proc()
  end

  def initialize_scene

    cam = M3G::Camera.new
    cam.look_at  0,0,5, 0,0,0, 0,1,0

    bg = M3G::Background.new
    bg.color = 0x7f3f3f3f


    positions = M3G::VertexArray.new 4, 3, 2
    positions.values[0,4] = [1,-1,0, 1,1,0, -1,-1,0, -1,1,0]

    scale = 1
    bias  = [0,0,0]
    vertices = M3G::VertexBuffer.new
    vertices.positions = positions, scale, bias

    indices = [0,1,2,3]
    strips  = [4]
    tris = M3G::TriangleStripArray.new indices, strips
    
    comp = M3G::CompositingMode.new
    comp.blending = M3G::CompositingMode::ALPHA

    mat = M3G::Material.new
    mat.color[M3G::Material::DIFFUSE] = 0x7f0000ff

    app = M3G::Appearance.new
    app.compositing_mode = comp
    app.material = mat

    keyframe_alpha = M3G::KeyframeSequence.new 2, 1, M3G::KeyframeSequence::LINEAR
    frame_alpha = [[1], [0.1]]
    keyframe_alpha.keyframe[0] = 0, frame_alpha[0]
    keyframe_alpha.keyframe[1] = 100, frame_alpha[1]
    keyframe_alpha.valid_range = 0, 1
    keyframe_alpha.duration    = 100

    keyframe_color = M3G::KeyframeSequence.new 3, 3, M3G::KeyframeSequence::LINEAR
    frame_color = [[0,0,1], [1,0,0], [0,0,1]]
    keyframe_color.keyframe[0] = 0, frame_color[0]
    keyframe_color.keyframe[1] = 100, frame_color[1]
    keyframe_color.keyframe[2] = 200, frame_color[2]
    keyframe_color.repeat_mode = M3G::KeyframeSequence::LOOP
    keyframe_color.valid_range = 0, 2
    keyframe_color.duration    = 200

    controller = M3G::AnimationController.new
    controller.speed = 0.5, 0
    
    track_alpha = M3G::AnimationTrack.new keyframe_alpha, M3G::AnimationTrack::ALPHA
    track_color = M3G::AnimationTrack.new keyframe_color, M3G::AnimationTrack::DIFFUSE_COLOR
    track_alpha.controller = controller
    track_color.controller = controller

    #mat.add_animation_track track_alpha
    mat.animation_track << track_color

    mesh = M3G::Mesh.new vertices, tris, app

    @wld = M3G::World.new
    @wld.child << cam
    @wld.child << mesh
    @wld.active_camera = cam
    @wld.background    = bg

    @world_time = 0
  end

  def reshape width, height
    g3d = M3G::Graphics3D::instance
    g3d.viewport = 0,0,width,height
    @wld.active_camera.perspective = 45, width/(height*1.0), 0.1, 100
  end

  def display
    g3d = M3G::Graphics3D::instance
    g3d.render @wld
    GLUT.SwapBuffers()
  end
  
  def keyboard key, x, y
    if key == 113  # 'q'
      exit
    elsif key == 32 # ' '
      @wld.animate @world_time
      @world_time += 2
    end
    GLUT.PostRedisplay
  end

  def mouse button, state, x, y
  end

  def idle
    GLUT.PostRedisplay
  end

  def start
    GLUT.MainLoop
  end

end


MyGLUTWidget.new.start

